package hyo.hun.lifemanager;

import hyo.hun.lifemanager.common.SuperInterface;
import hyo.hun.lifemanager.connect.CalendarConnect;
import hyo.hun.lifemanager.connect.CalendarInfo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CalendarSelectDialogActivity extends Activity implements SuperInterface
{
	private CalendarConnect calendarConnect;
	
	private ListView listView;
	private ArrayList<CalendarInfo> calendarList;
	private ArrayList<String> list;
	private ArrayAdapter<String> adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
	    setContentView(R.layout.calendar_select_dialog_activity);
	    
	    calendarConnect = CalendarConnect.getCalendarConnect();
	    
	    setFindView();
	    setDataSetting();
	    setClickListener();
	    
	}
	
	@Override
	public void setFindView() {
		listView = (ListView) findViewById(R.id.calendar_select_dialog_activity_list);
	}

	@Override
	public void setDataSetting() {
		
		calendarList = new ArrayList<CalendarInfo>();
		list = new ArrayList<String>();
		
		for (int i=0; i<calendarConnect.getCalendarList().size(); i++)
		{
			list.add(calendarConnect.getCalendarList().get(i).toString());
		}
		
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

		listView.setAdapter(adapter);
				
	}

	@Override
	public void setClickListener() {
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) 
			{
				for (int i=0; i<calendarConnect.getCalendarList().size(); i++)
				{
					if (calendarConnect.getCalendarList().get(i).toString() == list.get(position))
					{
						Intent intent = new Intent();
						intent.putExtra("asdf", calendarConnect.getCalendarList().get(i).getId());
						setResult(Activity.RESULT_OK, intent);
						
						finish();
					}
				}
			}
		});
	}

}
